<template>
  <BasicModal
    v-bind="$attrs"
    width="55%"
    :height="370"
    title="赠送优惠券"
    @register="register"
    @ok="submit"
  >
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn" />
  </BasicModal>
</template>
<script setup>
import { ref } from 'vue';
import { BasicTable, useTable } from '@/components/Table';
import { BasicModal, useModalInner } from '@/components/Modal';
import { couPonColumns } from '../../member/data';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
const { createMessage } = useMessage();
//选中部门主键
const propsData = ref();
const emit = defineEmits(['selectedConpon']);
const [register, { closeModal }] = useModalInner((data) => {
  propsData.value = data.memberIds;
});
const [registerTable, { reload, getSelectRows }] = useTable({
  api: '/coupon/coupon/setting',
  // beforeFetch: () => {
  //   // 接口请求前 参数处理
  //   // var param = {
  //   //   id: propsData.value.deptId,
  //   // };
  //   return param;
  // },
  columns: couPonColumns,
  useSearchForm: true,
  maxHeight: 200,
  pagination: true,
  formConfig: {
    labelWidth: 30,
    schemas: [
      {
        field: 'name',
        showAdvancedButton: true,
        label: '优惠券名称',
        hideLabel: false,
        component: 'Input',
        required: false,
      },
    ],
    baseColProps: { span: 6 },
  },
  bordered: true,
  showIndexColumn: false,
  immediate: true,
  striped: true,
  showTableSetting: false,
  rowKey: 'id',
  rowSelection: {
    type: 'checkbox',
  },
});
// 确定提交
async function submit() {
  if (getSelectRows().length > 0) {
    let couponIds = [];
    for (let a = 0; a < getSelectRows().length; a++) {
      couponIds.push(getSelectRows()[a].id);
    }
    await defHttp.post({
      url: '/admin/member/giveCoupons',
      data: JSON.stringify({
        memberids: propsData.value,
        couponIds: couponIds,
      }),
    });
    closeModal();
    createMessage.success('已赠送');
  } else {
    createMessage.error('请选择要赠送的优惠券');
  }
}
const handleResizeColumn = (w, col) => {
  col.width = w;
};
</script>
